package handler

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"github.com/gocolly/colly"
	"log"
	"sbd_nydjz/models"
	"sbd_nydjz/service"
	"sbd_nydjz/utils"
	"strings"
)

type TagViewHandler struct {
	TageViewSrv service.TagViewService
	SyfwSrv     service.SyfwService
}

func (handler TagViewHandler) InsterTagViewHandler(djzh string) {
	isTagView := &models.Tagview{Djzh: djzh}
	isSyfw := &models.Syfw{Djzh: djzh}
	// fmt.Println(id)
	url := fmt.Sprintf("https://www.icama.cn/BasicdataSystem/pesticideRegistration/tagview.do?pdno=%s", djzh)
	// fmt.Println(url)
	// isGaster := &models.Djzinfo{}
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))
	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=D4F6BAB7AE0FC35310CF83EB3ECDF014; pageSize=30; pageNo=1")
		// r.Headers.Set("Content-Type", " application/x-www-form-urlencoded")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		nodes := htmlquery.Find(doc, `/html/body/div[1]/table/tbody/tr/td/table/tbody/tr`)
		// html/body/div[1]
		num := len(nodes)
		for key, node := range nodes {
			// fmt.Println(key)
			if key == num-10 {
				// 使用技术要求:
				Syfw := htmlquery.Find(node, "./td/table/tbody/tr")
				for syfw_key, syfw_node := range Syfw {
					if syfw_key < 1 {
						continue
					}
					Zw := htmlquery.FindOne(syfw_node, "./td[1]/span")
					isSyfw.Zw = utils.ReplaceStr(htmlquery.InnerText(Zw))
					Fzdx := htmlquery.FindOne(syfw_node, "./td[2]/span")
					isSyfw.Fzdx = utils.ReplaceStr(htmlquery.InnerText(Fzdx))
					Yyl := htmlquery.FindOne(syfw_node, "./td[2]/span")
					isSyfw.Yyl = utils.ReplaceStr(htmlquery.InnerText(Yyl))
					Syfs := htmlquery.FindOne(syfw_node, "./td[2]/span")
					isSyfw.Syfs = utils.ReplaceStr(htmlquery.InnerText(Syfs))
				}
				isSyfw.ID = utils.SnowId()
				handler.SyfwSrv.InsterSyfwSrv(*isSyfw)
				// isSyfw.Zw = utils.ReplaceStr(htmlquery.InnerText(Syfw[]))

				// fmt.Println(htmlquery.OutputHTML(Syfw[0], false))
			}

			if key == num-9 {
				// 使用技术要求:
				Syjsyq := htmlquery.FindOne(node, "./td/span[2]")
				isTagView.Syjsyq = utils.ReplaceStr(htmlquery.InnerText(Syjsyq))

				// fmt.Println(isTagView.Syjsyq)
			}
			if key == num-8 {
				// 产品性能:
				Cpxn := htmlquery.FindOne(node, "./td/span[2]")
				isTagView.Cpxn = utils.ReplaceStr(htmlquery.InnerText(Cpxn))
			}
			if key == num-7 {
				// 注意事项
				Zysx := htmlquery.FindOne(node, "./td/span[2]")
				isTagView.Zysx = utils.ReplaceStr(htmlquery.InnerText(Zysx))
			}

			if key == num-6 {
				// 中毒急救措施：
				Zdjjcs := htmlquery.FindOne(node, "./td/span[2]")
				isTagView.Zdjjcs = utils.ReplaceStr(htmlquery.InnerText(Zdjjcs))
			}
			if key == num-5 {
				// 储存和运输方法
				Cchysff := htmlquery.FindOne(node, "./td/span[2]")
				isTagView.Cchysff = utils.ReplaceStr(htmlquery.InnerText(Cchysff))
			}

			if key == num-4 {
				// 质量保证期
				Bzq := htmlquery.FindOne(node, "./td/span[2]")
				isTagView.Bzq = utils.ReplaceStr(htmlquery.InnerText(Bzq))
			}

		}
		// /html/body/div[2]/table/tbody/tr/td/table/tbody/tr[15]
		// fmt.Println(isTagView.Bzq)
		isTagView.ID = utils.SnowId()
		handler.TageViewSrv.InsterTagViewSrv(*isTagView)
	})
	c.Visit(url)
}
